#include "stdafx.h"
#include <LitFileParser.h>

namespace Lit
{
	FileLineParser::FileLineParser( const String &filename, bool binary )
		: mRefLine( 0 )
	{
		open( filename, binary );
	}
	FileLineParser::FileLineParser( )
		: mRefLine( 0 )
	{
	}
	FileLineParser::~FileLineParser( )
	{
		close();
	}
	FileLineParser::operator bool() const{ return isGood(); }

	bool FileLineParser::isOpen() const{ return mStream.is_open(); }
	bool FileLineParser::isGood() const{ return mStream.good(); }
	size_t FileLineParser::getCurrentLine( ) const
	{
		return mRefLine;
	}
	bool FileLineParser::eof() const{ return mStream.eof(); }
	FileLineParser& FileLineParser::operator >>( Line& line )
	{
		if( !eof() )
		{
			std::string ln;
			std::getline( mStream, ln );
			line.line = ln.c_str();
			line.id = mRefLine;
			mRefLine ++;
		}
		return *this;
	}
	FileLineParser& FileLineParser::operator >>( String& line )
	{
		if( !eof() )
		{
			std::string str;
			std::getline( mStream, str );
			line = str.c_str();
			mRefLine ++;
		}
		return *this;
	}
	bool FileLineParser::open( const String &filename, bool binary ){
		close();
		mStream.open( filename.data(), (binary?(std::ios::in|std::ios::binary):(std::ios::in)) );
		return mStream.is_open();
	}
	bool FileLineParser::close()
	{
		if( mStream.is_open() ){ mStream.close(); return true; }
		return false;
	}
};
